package com.block.api.controller.expose;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.Charset;

@RestController
@RequestMapping("test")
public class TestController {

    /**
     * test/getresp
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getresp")
    public String test() throws Exception {
        ThreadUtil.safeSleep(3000);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", "200");
        jsonObject.put("message", "成功");
        return jsonObject.toJSONString();
    }

    @RequestMapping(value = "/getWuYiWeiQianBao")
    public String getWuYiWeiQianBao() throws Exception {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 200);
        jsonObject.put("msg", "申请成功");
        return jsonObject.toJSONString();
    }

    @RequestMapping(value = "/apis/checkUser", method = {RequestMethod.POST, RequestMethod.GET})
    public String check( HttpServletRequest request, HttpServletResponse response) throws IOException {

       String  paramContent = IoUtil.read(request.getInputStream(), Charset.defaultCharset());
        System.out.println("==============撞库");
        System.out.println(paramContent);
        System.out.println("==============");

        JSONObject jsonObject = JSON.parseObject(paramContent);
        String tranData = jsonObject.getString("tranData");

        String result = SecureUtil.aes("J34EGQK264CJUVK2COSHG176VDYPAX7O".getBytes()).decryptStr(tranData);

        System.out.println("==============撞库解密完成");
        System.out.println(result);
        System.out.println("==============");

        JSONObject resultObject = JSON.parseObject(result);
        String outOrderNo = resultObject.getString("outOrderNo");

        JSONObject dataObject = new JSONObject();
        dataObject.put("outOrderNo", outOrderNo);
        dataObject.put("orderNo", RandomUtil.randomString(16));
        dataObject.put("productId", RandomUtil.randomInt(100));
        dataObject.put("productName", "有钱");
        dataObject.put("companyName", "花花呗");
        dataObject.put("companyLogo", "https://laiqianba-pub.oss-cn-beijing.aliyuncs.com/merchantlogo/0vm5xjhfjqhp092i.jpg");
        dataObject.put("maxLoanAmount", "300000");
        dataObject.put("maxLoanRate", "12");
        dataObject.put("minLoanRate", "2");
        dataObject.put("businessInfo", "信用贷款");
        dataObject.put("price", 30.00);//分润前价格

        JSONArray jsonArray = new JSONArray();

        JSONObject protocolInfo1 = new JSONObject();
        protocolInfo1.put("protocalUrl", "https://www.baidu.com");
        protocolInfo1.put("protocalName", "用户协议");

        JSONObject protocolInfo2 = new JSONObject();
        protocolInfo2.put("protocalUrl", "https://www.jd.com");
        protocolInfo2.put("protocalName", "隐私协议");
        jsonArray.add(protocolInfo1);
        jsonArray.add(protocolInfo2);

        dataObject.put("protocalList", jsonArray);

        JSONObject returnObject = new JSONObject();
        returnObject.put("respCode", "200");
        returnObject.put("respMsg", "成功");
        returnObject.put("respData", dataObject);

        System.out.println("==============撞库返回数据");
        System.out.println(JSON.toJSONString(returnObject));
        System.out.println("==============");

        return returnObject.toJSONString();
    }

    @RequestMapping(value = "/apis/registerUser", method = {RequestMethod.POST, RequestMethod.GET})
    public String register(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String  paramContent = IoUtil.read(request.getInputStream(), Charset.defaultCharset());
        System.out.println("==============register");
        System.out.println(paramContent);
        System.out.println("==============");

        JSONObject jsonObject = JSON.parseObject(paramContent);
        String tranData = jsonObject.getString("tranData");

        String result = SecureUtil.aes("J34EGQK264CJUVK2COSHG176VDYPAX7O".getBytes()).decryptStr(tranData);

        System.out.println("==============register解密完成");
        System.out.println(result);
        System.out.println("==============");

        JSONObject resultObject = JSON.parseObject(result);
        String outOrderNo = resultObject.getString("outOrderNo");
        String orderNo = resultObject.getString("orderNo");

        JSONObject dataObject = new JSONObject();
        dataObject.put("orderNo", orderNo);
        dataObject.put("status", "000");
        dataObject.put("message", "进件成功");

        JSONObject returnObject = new JSONObject();
        returnObject.put("respCode", "200");
        returnObject.put("respMsg", "成功");
        returnObject.put("respData", dataObject);

        System.out.println("==============register返回数据");
        System.out.println(JSON.toJSONString(returnObject));
        System.out.println("==============");

        return returnObject.toJSONString();
    }

}
